package MaxTopn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

// 1. 实现 Writable 接口
public class MaxTopFlowBean implements WritableComparable<MaxTopFlowBean> {
    private String symbol;
    private float open;
    private float close;
    private float sub;

    //2  反序列化时，需要反射调用空参构造函数
    public MaxTopFlowBean() {
    }

    //3  序列化方法
    @Override
    public void write(DataOutput dataOutput) throws IOException {
//        dataOutput.writeLong(upFlow);
//        dataOutput.writeLong(downFlow);
//        dataOutput.writeLong(sumFlow);
        dataOutput.writeUTF(symbol);
        dataOutput.writeFloat(open);
        dataOutput.writeFloat(close);
//        dataOutput.writeFloat(low);
//        dataOutput.writeFloat(high);
//        dataOutput.writeDouble(volume);
        dataOutput.writeFloat(sub);
    }

    //4 反序列化方法
    //5 反序列化方法读顺序必须和写序列化方法的写顺序必须一致
    @Override
    public void readFields(DataInput dataInput) throws IOException {
//        upFlow = dataInput.readLong();
//        downFlow = dataInput.readLong();
//        sumFlow = dataInput.readLong();
        symbol = dataInput.readUTF();
        open = dataInput.readFloat();
        close = dataInput.readFloat();
//        low = dataInput.readFloat();
//        high = dataInput.readFloat();
//        volume = dataInput.readDouble();
        sub = dataInput.readFloat();
    }

    // 6 编写toString方法，方便后续打印到文本
    @Override
    public String toString() {
//        return symbol + "\t" + open + "\t" + close + "\t" + low +"\t" + high + "\t" + volume;
        return symbol +"\t" + open + "\t" + close + "\t" + sub;
    }

    public MaxTopFlowBean(float open, float close, float sub, String symbol) {
        this.symbol = symbol;
        this.open = open;
        this.close = close;
//        this.low = low;
//        this.high = high;
//        this.volume = volume;
        this.sub = close - open;
    }

//    public

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public float getOpen() {
        return open;
    }

    public void setOpen(float open) {
        this.open = open;
    }

    public float getClose() {
        return close;
    }

    public void setClose(float close) {
        this.close = close;
    }

//    public float getLow() {
//        return low;
//    }

//    public void setLow(float low) {
//        this.low = low;
//    }

//    public float getHigh() {
//        return high;
//    }

//    public void setHigh(float high) {
//        this.high = high;
//    }

//    public double getVolume() {
//        return volume;
//    }

//    public void setVolume(double volume) {
//        this.volume = volume;
//    }

    public float getSub() {
        return sub;
    }

    public void setSub(float sub) {
        this.sub = sub;
    }

    //试比较volume
    @Override
    public int compareTo(MaxTopFlowBean o) {
        int result;

//        result = Float.compare(this.sub, o.getSub());
        result = Float.compare(o.getSub(), this.sub);

        return result;
    }
}
